import { getProfileAPI, loginAPI } from '@/api/user'
import { removeToken, setToken } from '@/utils/auth'

export default {
  namespaced: true,
  state: {
    token: ''
    // profile: {}
  },
  mutations: {
    set_Token(state, newToken) {
      state.token = newToken
      setToken(newToken)
    },
    removeToken(state) {
      state.token = null
      removeToken()
    },
    setProfile(state, profile) {
      state.profile = profile
    },
    clearUserInfo(state) {
      // 清除Token
      state.token = ''
      state.profile = {}
      removeToken()
    }
  },
  actions: {
    async Sendlogin(etx, param) {
      const res = await loginAPI(param)
      etx.commit('set_Token', res.data.token)
      console.log(res.data.token)
    },
    async getProfile(ctx) {
      const res = await getProfileAPI()
      ctx.commit('setProfile', res.data)
      return res.data.permissions
    }

    // actions: {
    //   async getProfile(ctx) {
    //     const res = await getProfileAPI()
    //     ctx.commit('setProfile', res.data)
    //   }
    // }
  }
}
